package com.tz.controller;

import com.tz.entity.Research;
import com.tz.entity.ResearchDetail;
import com.tz.entity.json.PollDetails;
import com.tz.service.ResearchDetailService;
import com.tz.service.ResearchService;
import com.tz.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 侯畅畅 on 2018/1/4.
 */
@Controller
@RequestMapping("/research")
public class ResearchController {
    @Autowired
    ResearchService researchService;
    @Autowired
    ResearchDetailService researchDetailService;
    @ResponseBody
    @RequestMapping(value = "/showResearch", produces = "application/json")
    public PageBean<Research> showResearch(){
        return researchService.findResearchsByPage(1,7,1,null);
    }
    @ResponseBody
    @RequestMapping(value = "/showDetail", produces = "application/json")
    public Research showDetail(@RequestBody Long researchid){
        Research research = researchService.findById(researchid);
        List<ResearchDetail> researchDetails = researchDetailService.gainAllDetails(researchid);
        research.setResearchDetails(researchDetails);
        System.out.println(research);
        return  research;

    }
    @ResponseBody
    @RequestMapping(value = "/showOneResearchDetail", produces = "application/json")
    public List<PollDetails> showOneResearchDetail(@RequestBody Long researchDetailid){
        ResearchDetail researchDetail = researchDetailService.selectDetailById(researchDetailid);
        return researchDetailService.getPollDetails(researchDetail);
    }
    @ResponseBody
    @RequestMapping(value = "/alertResearchDetail")
    public String alertResearchDetail(@RequestBody String researchDetailid){
        System.out.println(researchDetailid);
        String[] strings = researchDetailid.split(";");
        Long id  = Long.parseLong(strings[1]);
        Research research = researchService.findById(id);
        System.out.println(research);
        //修改状态
        research.setState(2);
        if(strings[0].length()>0){
            String[] strings1 = strings[0].split(":",0);
            System.out.println(strings1);
            List<Long> detailid = new ArrayList<>();
            for(int i = 0;i<strings1.length;i++){
                detailid.add(Long.parseLong(strings1[i]));
            }
            //修改Detail表中的数据
            researchDetailService.alterResearchDetails(detailid);
            //修改Research的状态,有效份数,平均分
            //调用得到平均分的方法
            double avgs = researchDetailService.selectAVGByResearchID(id);
            int effectivenum = research .getTotalnum()-strings1.length;
            research.setEffectivenum(effectivenum);
            //修改平均分
            research.setAver(avgs);
        }
        researchService.alterResearch(research);
        return "success";
    }
    @ResponseBody
    @RequestMapping(value = "/getResearch", produces = "application/json")
    public PageBean<Research> getResearch(@RequestBody String string){
        System.out.println(researchService.findResearchsByPage(1,7,1,string));
        return researchService.findResearchsByPage(1,7,1,string);
    }
    @ResponseBody
    @RequestMapping(value = "/researchlist", produces = "application/json")
    public PageBean<Research> getList(int pageNow,int pageSize){
        System.out.println(researchService.findResearchsByPage(pageNow,pageSize,1,null));
        return researchService.findResearchsByPage(pageNow,pageSize,1,null);
    }
}